package cn.zcib.film.service.impl;

import cn.zcib.film.anno.Record;
import cn.zcib.film.bean.Admin;
import cn.zcib.film.bean.Role;
import cn.zcib.film.bean.vo.AdminVO;
import cn.zcib.film.mapper.AdminMapper;
import cn.zcib.film.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Admin login(String aName) {
        Admin result = adminMapper.getAdminByName(aName);
        return result;
    }

    @Override
    public Admin getAdminById(Integer aId) {
        return adminMapper.getAdminByaId(aId);
    }

    @Override
    public List<Admin> findAdmins(AdminVO vo) {
        //处理模糊查询参数
        if (vo.getaName()!=null && vo.getaName()!=""){
            vo.setaName("%"+vo.getaName()+"%");
        }
        List<Admin> admins = adminMapper.getAdmins(vo);
        return admins;
    }

    @Override
    public int getAdminCountByRoleId(Integer rId) {
        return adminMapper.getAdminCountByRoleId(rId);
    }

    @Override
    public int saveAdminRole(Integer aId, Integer rId) {
        return adminMapper.saveAdminRole(aId, rId);
    }

    @Override
    public Boolean modPwd(Integer aId, Integer newPassword,Integer oldPassword) {
        Admin admin = adminMapper.getAdminByIdAndPwd(aId, oldPassword);
        if (Objects.isNull(admin)){
            return false;
        }else {
            int row = adminMapper.updateAdmin(aId, newPassword);
            if (row < 1 ){
                return false;
            }
        }
        return true;
    }

}
